<?php 
ob_start();
session_start();

include('gest_login.php');
include('languages/it.php');

//recupero del cookie se c'è
if (isset($_COOKIE["ragusainfoto"]))
	get_session($_COOKIE["ragusainfoto"]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<noscript><meta http-equiv="refresh" content="0; url=jserror.html"></noscript>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>RagusaInFoto</title>
<script language="Javascript" src="js/functions.js"></script> 
<script src="http://connect.facebook.net/it_IT/all.js"></script> 
</head>
<body>
	<?php if (isset($_SESSION['id'])){ ?>
	 <p><?php echo $_SESSION['user']; 
	 if($_SESSION['id_fb']) 
	 	echo("<img src=\"images/facebook.png\" alt=\"facebook\"/>")
	 ?> | <a href="load.php">Carica Foto</a> | <a href="profile.php">Profilo</a> | <a href="logout.php">Logout</a>
	<?php 
		if ($_SESSION['admin'])
		{
		?> | <a href="admin/">Admin</a>
	<?php
		}
	}
	else { ?>
		<form id="login" method="post" name="login" onsubmit="return check_form_login(this)">
		<p>Mail:
		<input type="text" name="mail" id="mail">
		Password:
		<input type="password" name="pass" id="pass">
		<input type="submit" name="login" value="Login">
		 |  <a href="fb_connect.php">Facebook Login</a>
		 | <a href="registration/">Registrati</a>
		</p>
		</form>
	<?php } 
	
	//Facebook Login
	if (isset($_REQUEST["code"]))
	{
	$app_id = "163254560388429";
	$app_secret = "214cad1b0321b423e25c5abad7e58a32";
	$my_url = "http://www.giorgioprovenzale.it/sites/ragusainfoto/index.php";
	
	$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
        . $app_secret . "&code=" . $code;

    $access_token = file_get_contents($token_url);

    $graph_url = "https://graph.facebook.com/me?" . $access_token;

    $user = json_decode(file_get_contents($graph_url));
    
    $id_fb = (string)$user->id;
    $name = (string)$user->first_name;
    $username = (string)$user->name;
    $surname = (string)$user->last_name;
    $mail = (string)$user->email; 
    $place = (string)$user->location->name; 
    
    check_login_fb($username, $name, $surname, $mail, $id_fb, $place);
	}
	//Fine Facebook Login
	
	
if(isset($_POST['mail']))
{
	check_login($_POST['mail'], $_POST['pass']);
}
if(isset($_GET['code_err']))
{
	$code = $_GET['code_err'];
	
	if($code == 2)//login incorretto
		echo "<p>".INCORRECT_LOGIN."</p>";
		
	if($code == 3)//utente non verificato
		echo "<p>".NOT_VERIFICATED_USER."</p>";
		
	if($code == 4)//non è un errore ma il messaggio che è stata inviata una mail per la verifica del nuovo indirizzo mail inserito
		echo "<p>".MAIL_CHANGED."</p>";
		
	if($code == 5)//non è un errore ma il messaggio per utente cancellato
		echo "<p>".DELETED_USER."</p>";
}
?>
</body>
</html>
